package com.sgb.goods.viewmodel.inquiry;

import androidx.lifecycle.MutableLiveData;

import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.inquiry.PriceCheckListEntity;
import com.sgb.goods.entity.inquiry.UrlEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.utils.module.GoodsModule;
import com.sgb.goods.view.adapter.inquiry.MyInquiryAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;

import java.util.List;


/**
 * 描述：个人中心--我的询价--询价列表
 *
 * @author qiangHeNooo
 * 时间：2021.10.28
 */
public class MyInquiryViewModel extends BaseViewModel {

    public MutableLiveData<List<PriceCheckListEntity>> priceCheckListEntityObservableField = new MutableLiveData<>();
    public MyInquiryAdapter myInquiryAdapter;
    private int page = 1;

    public MyInquiryViewModel() {
        this.myInquiryAdapter = new MyInquiryAdapter();
        this.myInquiryAdapter.setOnItemClickListener((MyInquiryAdapter.OnItemChildClickListener) (view, entity, position) -> getPriceCheckDetail(entity.no, entity.status,entity.itemName));
    }

    /**
     * 查询我的询价列表
     */
    public void getPriceCheckList(String catName, int status, boolean isLoadMore ,boolean isShowDialog) {
        page = isLoadMore ? page + 1 : Constants.PAGE_NUM;
        if(isShowDialog){
            showLoading();
        }
        GoodsNet.getInstance().getInquiryApi().getPriceCheckList(catName, page, Constants.PAGE_SIZE_10, status)
                .enqueue(new GoodsCallBack<BaseEntity<ListResponse<List<PriceCheckListEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponse<List<PriceCheckListEntity>>> data) {
                        if (data == null || data.getData() == null) {
                            priceCheckListEntityObservableField.postValue(null);
                        } else {
                            if (isLoadMore) {
                                myInquiryAdapter.addData(data.getData().getList());
                            } else {
                                myInquiryAdapter.setNewData(data.getData().getList());
                            }
                            priceCheckListEntityObservableField.postValue(data.getData().getList());
                        }
                    }

                    @Override
                    public void onFail() {
                        priceCheckListEntityObservableField.postValue(null);
                    }
                });
    }

    /**
     * 询价详情
     */
    private void getPriceCheckDetail(String id, String state,String itemname) {
        showLoading();
        GoodsNet.getInstance().getInquiryApi().getUrlSetting("pensonalEnpuiryDetail").enqueue(new GoodsCallBack<BaseEntity<UrlEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UrlEntity> data) {
                String url = data.getData().getUrl() + "?no=" + id;
                showUrl(url, id, "1", state,itemname);
            }
        });
    }

    public void showUrl(String url, String id, String type, String state,String sharTitleText ) {
        GoodsModule.getInstance().call1("jumpWebViewX5HostActivity", url, id, type, state,sharTitleText);
    }

}
